<?php

/**
 * Created by PhpStorm.
 * User: bob
 * Date: 21/9/25
 * Time: 上午10:37
 */

namespace App\Api\Peipei\Controllers;

use App\Api\Account\Services\BusinessService;
use App\Api\Peipei\Models\Order;
use App\Api\Web\Models\Bbs;
use App\Api\Web\Services\BbsService;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;

class PeipeiController extends Controller
{
    /**
     * 录单
     * @param Request $request
     * @return array
     */
    public function order(Request $request)
    {
        $params = $request->all();
        $user = authUser();

        $roles = data_get(BusinessService::getUserRole($user), 'name');
        if (!in_array('cs', $roles)) {
            throw new \LogicException('抱歉，您没有录单权限', 403);
        }
        $peiId = data_get($params, 'pei_id', '');
        if (empty($peiId)) {
            throw new \LogicException('请先选择陪陪', 100);
        }

        $pei = \DB::table('users as u')
            ->join('role_user as ru', 'ru.user_id', '=', 'u.id')
            ->where('ru.role_id', '=', 2)
            ->select('u.name')
            ->first();

        if (empty($pei)) {
            throw new \LogicException('请重新选择陪陪', 110);
        }

        $peiName = data_get($pei, 'name', '');

        $shouTotal = data_get($params, 'shou_total', 0);
        $xuTotal = data_get($params, 'xu_total', 0);
        $shangTotal = data_get($params, 'shang_total', 0);

        $peiShouTotal = round($shouTotal * 0.5, 2);
        $peiXuTotal = round($xuTotal * 0.6, 2);
        $peiShangTotal = round($shangTotal * 0.7, 2);

        $peiTotal = bcadd(bcadd($peiShouTotal, $peiXuTotal, 2), $peiShangTotal, 2);
        $total = bcadd(bcadd($shouTotal, $xuTotal, 2), $shangTotal, 2);

        $csTotal = round($total * 0.12, 2);

        $r = Order::create([
            'cs_id' => $user->id,
            'pei_id' => $peiId,
            'pei_name' => $peiName,
            'shou_total' => $shouTotal,
            'xu_total' => $xuTotal,
            'shang_total' => $shangTotal,
            'total' => $total,
            'pei_shou_total' => $peiShouTotal,
            'pei_xu_total' => $peiXuTotal,
            'pei_shang_total' => $peiShangTotal,
            'pei_total' => $peiTotal,
            'cs_total' => $csTotal,
        ]);

        BusinessService::commonTransaction($user->id, $csTotal, data_get($r, 'id'), 'order', '客服'.$user->name.'录入'.$peiName.'订单');
        BusinessService::commonTransaction($peiId, $peiTotal, data_get($r, 'id'), 'order', '客服'.$user->name.'录入'.$peiName.'订单');

        return $this->formatOutput($r);
    }

    public function createUser(Request $request)
    {
        $params = $request->all();
        $name = data_get($params, 'name', '');
        $password = data_get($params, 'password', '');
        $sex = data_get($params, 'sex', 1);
        $level = data_get($params, 'level', '');
        $role = data_get($params, 'role', 2);

        $r = User::create([
            'name' => $name,
            'password' => bcrypt($password),
            'sex' => $sex,
            'level' => $level
        ]);

        \DB::table('role_user')->create([
            'user_id' => data_get($r, 'id'),
            'role_id' => $role,
        ]);

        return $this->formatOutput($r);

    }

    public function getLevel(Request $request)
    {
        $config = config('vip.peipei_level', []);

        return $this->formatOutput($config);

    }

    public function getPeipei(Request $request)
    {
        $params = $request->all();
        $name = data_get($params, 'name', '');
        $m = \DB::table('users as u')
            ->join('role_user as ru', 'ru.user_id', '=', 'u.id')
            ->select('u.id', 'u.name');
        if (!empty($name)) {
            $m = $m->where('u.name', 'like', "%$name%");
        }
        $m = $m
            ->where('ru.role_id', '=', 3)
            ->get();

        return $this->formatOutput($m);
    }


}
